package club.wx3609.images.controller;

import club.wx3609.images.Reptile.service.GetImages;
import club.wx3609.images.Utils.IsNullUtils;
import club.wx3609.images.entity.Image;
import club.wx3609.images.entity.ImageUrls;
import club.wx3609.images.entity.User;
import club.wx3609.images.interfaces.Constant;
import club.wx3609.images.service.ImageService;
import club.wx3609.images.service.ImageUrlService;
import org.springframework.data.domain.Page;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@Controller
public class ImageController {

    @Resource
    private ImageUrlService imageUrlService;
    @Resource
    private ImageService imageService;
    @Resource
    private GetImages getImages;

    private Integer pageSize=10;
    private String sortTitle="imageId";

    @Transactional
    @ResponseBody
    @PostMapping("/Resources/Images/addImage")
    public String addImage(String urlName,String ImageTag,String ImageNameTag){
        if(IsNullUtils.character(urlName)&&IsNullUtils.character(ImageTag)){
            boolean judgeInput=Pattern.matches("^(http://|https://)[0-9a-zA-Z]+.[0-9a-zA-Z]+.[0-9a-zA-Z]+(/|//?[0-9a-zA-Z]+)?",urlName);
            if(judgeInput){
                ImageUrls imageurls=imageUrlService.findByImageUrl(urlName);
                if(imageurls==null) {
                    ImageUrls imageUrls = new ImageUrls();
                    imageUrls.setUrlName(urlName);
                    imageUrls.setImageTag(ImageTag);
                    imageUrls.setImageNameTag(ImageNameTag);
                    imageUrlService.addImageUrl(imageUrls);
                }
                int result=0;
                try {
                    result=getImages.ObtainImage(getImages.getImageMessage(urlName, ImageTag, ImageNameTag));
                    Thread.sleep(1000);
                }catch(Exception e){
                    e.printStackTrace();
                }
                if(result>0) {
                    return Constant.reptileState;
                }else {
                    return Constant.reptileState1;
                }
            }
            return Constant.inputJudge1;
        }
        return Constant.inputJudge0;
    }

    @GetMapping("/Resources/Images/imagesManage/{Id}")
    public String ImagesManage(@PathVariable("Id") Integer Id, Map<String,Object> map, HttpServletRequest request){
        Object object=request.getSession().getAttribute("User");
        if(object!=null) {
            User user=(User)object;
            Page<Image> page=imageService.findAllImage(Id,pageSize,sortTitle);
            List<Image> images=imageService.findAll();
            Integer pageNumbers=0;
            int num=(images.size())%pageSize;
            pageNumbers=Integer.valueOf((images.size())/pageSize);
            if(num>0){
                pageNumbers++;
            }
            if(pageNumbers>0){
                pageNumbers--;
            }
            map.put("pageNumbers",pageNumbers);
            map.put("pageNumber",Id);
            map.put("page",page);
            map.put("User",user);
            return "ImagesManage";
        }
        return "error";
    }
    @Async
    @ResponseBody
    @Transactional
    @PostMapping("/Resources/Images/deleteImage")
    public void deleteImage(Integer Id, Map<String,Object> map, HttpServletRequest request){
        Image image=imageService.findById(Id);
        String imageLocalPath=image.getImageLocalPath();
        String ImageLocalPath=(imageLocalPath).substring(1,imageLocalPath.length());
        File file=new File(ImageLocalPath);
        file.delete();
        imageService.deleteImage(Id);
    }
}
